﻿package src.uipackage.ui {
	
	import com.onebyonedesign.ui.complexbuttons.ComplexAccordionButton;
	import com.onebyonedesign.ui.complexbuttons.events.ComplexButtonEvent;
	import com.onebyonedesign.ui.complexbuttons.Menu;
	import com.onebyonedesign.ui.graphics.AccordionSection;
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.text.TextFormat;
	
	/**
	* OBO_Accordion is a UI Accordion widget.
	* @author Devon O.
	*/
	public class OBO_Accordion extends Sprite {
		
		private var _buttonInfo:Array;
		private var _numSections:int;
		private var _contentHeight:Number;
		private var _buttonWidth:Number;
		private var _buttonHeight:Number;
		private var _topGradient:uint;
		private var _bottomGradient:uint;
		private var _font:String;
		private var _fontSize:int;
		private var _fontColor:uint;
		private var _useRoundedCorners:Boolean;
		
		private var _sections:Array;
		private var _menu:Menu;
		private var _currentSelected:int = 0;
		
		/**
		 * The OBO_Accordion class creates a UI Accordion instance used to display various sections of content in a small area.
		 * 
		 * To use, the OBO_Accordion constructor should be passed an Array of Object instances. Each Object within the Array should contain a "label" property, which is a String to display in the Accordion button, and a "content" property, a DisplayObject instance (Sprite, MovieClip, or Shape) which will be displayed within that Accordion section.
		 * <br/><br/>
		 * This class requires Tweener. It can be downloaded at <a href="http://code.google.com/p/tweener/" target="_blank">http://code.google.com/p/tweener/</a>
		 * <br/><br/>
		 * Example:
		 * 
		 * <listing version="3.0">
		 *	package {
		 *
		 *		import com.onebyonedesign.ui.OBO_Accordion;
		 *		import flash.display.Sprite;
		 *		import flash.filters.DropShadowFilter;
		 *		import flash.text.TextField;
		 *		import flash.text.TextFieldAutoSize;
		 *		
		 *		public class AccordionTest extends Sprite {
		 *			
		 *			private var _accordion:OBO_Accordion;
		 *			
		 *			public function AccordionTest():void {
		 *				var content1:Sprite = createContent();
		 *				var content2:Sprite = createContent();
		 *				var content3:Sprite = createContent();
		 *				
		 *				var accordionInfo:Array = new Array( { label:"Content One", content:content1 }, { label:"Content Two", content:content2 }, { label:"Content Three", content:content3 } );
		 *			
		 *				_accordion = new OBO_Accordion(accordionInfo);
		 *				
		 *				_accordion.x = int(stage.stageWidth * .5 - _accordion.width * .5);
		 *				_accordion.y = int(stage.stageHeight * .5 - _accordion.height * .5);
		 *				
		 *				
		 *				addChild(_accordion);
		 *				
		 *				// getItemAt() method allows access to Accordion content
		 *				_accordion.getItemAt(0).getChildByName("textField").text = "This is text added after accordion creation.";
		 *			}
		 *
		 *			// Just creates a randomly colored sprite as test content
		 *
		 *			private function createContent():Sprite {
		 *				var content:Sprite = new Sprite();
		 *				content.graphics.beginFill(Math.random() * 0xFFFFFF);
		 *				content.graphics.drawRect(0, 0, 350, 200);
		 *				content.graphics.endFill();
		 *				
		 *				var tf:TextField = new TextField();
		 *				tf.autoSize = TextFieldAutoSize.LEFT;
		 *				tf.x = tf.y = 25;
		 *				tf.name = "textField";
		 *				tf.text = "This is text added during content creation.";
		 *				
		 *				content.addChild(tf);
		 *				
		 *				return content;
		 *			}
		 *		}
		 *	}
		 * </listing>
		 * @param	Array of objects each containing a label and content property.
		 * @param	Height of content instances.
		 * @param	Width of button and content.
		 * @param	Height of each button.
		 * @param	Top color of each button.
		 * @param	Bottom color of each button.
		 * @param	The String name of the font to use in each button.
		 * @param	The size of the above button font.
		 * @param	The color of the above button font.
		 * @param	Whether or not each button should have rounded corners.
		 */
		public function OBO_Accordion(buttonInfo:Array, contentHeight:Number = 200, btnWidth:Number = 350, btnHeight:Number = 30, topGradient:uint = 0xFFFFFF, bottomGradient:uint = 0xE5E5E5, font:String = "_sans", fontSize:int = 14, fontColor:uint = 0x000000, useRoundedCorners:Boolean = false):void {
			_buttonInfo = buttonInfo;
			_numSections = buttonInfo.length;
			_contentHeight = contentHeight;
			_buttonWidth = btnWidth;
			_buttonHeight = btnHeight;
			_topGradient = topGradient;
			_bottomGradient = bottomGradient;
			_font = font;
			_fontSize = fontSize;
			_fontColor = fontColor;
			_useRoundedCorners = useRoundedCorners;
			init();
		}
		
		/**
		 * 
		 * @param	The index of the item to retrieve (the first in the Accordion is 0, the second is 1, etc.).
		 * @return	Returns a reference to the content item (Sprite, MovieClip or Shape) at the specified index.
		 */
		public function getItemAt(index:int):* {
			return _menu.getItemAt(index);
		}
		
		private function init():void {
			_menu = new Menu();
			_sections = new Array();
			for (var i:int = 0; i < _numSections; i++) {
				var accSection:AccordionSection = new AccordionSection(_buttonInfo[i].label, _buttonInfo[i].content, _font, _fontSize, _fontColor, _contentHeight, _topGradient, _bottomGradient, _buttonWidth, _buttonHeight, _useRoundedCorners);
				accSection.data = i.toString();
				var section:ComplexAccordionButton = new ComplexAccordionButton(accSection);
				accSection.addEventListener(ComplexButtonEvent.ACCORDION_CHANGE, accordionHandler);
				_sections.push(accSection);
				_menu.addButton(section, 0, i * _buttonHeight);
			}
			_menu.selected = "0";
			_menu.addEventListener(Event.ENTER_FRAME, frameHandler);
			_menu.addEventListener(ComplexButtonEvent.RELEASE, menuHandler);
			addChild(_menu);
		}
		
		private function menuHandler(event:ComplexButtonEvent):void {
			_currentSelected = int(event.data);
		}
		
		private function accordionHandler(event:ComplexButtonEvent):void {
			var len:int = _sections.length;
			var currentButton:AccordionSection = _sections[_currentSelected];
			for (var i:int = 1; i < len; i++ ) {
				_sections[i].y = _sections[i - 1].y + _sections[i - 1].contentHeight;
			}
		}
		
		// This method is only used ONCE - on start up. All other accordion movements are handled by accordionHandler() method.
		private function frameHandler(event:Event):void {
			var len:int = _sections.length;
			var currentButton:AccordionSection = _sections[_currentSelected];
			for (var i:int = 1; i < len; i++ ) {
				_sections[i].y = _sections[i - 1].y + _sections[i - 1].contentHeight;
			}
			if (currentButton.contentHeight == _contentHeight) {
				_menu.removeEventListener(Event.ENTER_FRAME, frameHandler);
			}
		}
	}
}